// Directory Operations (目录操作) 示例
program DirectoryOperations;

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  DirName: string;
  SearchRec: TSearchRec;
  FindResult: LongInt;

begin
  WriteLn('=== Pascal 目录操作示例 ===');
  WriteLn;
  
  // 获取当前目录
  WriteLn('1. 当前工作目录:');
  WriteLn('   ', GetCurrentDir());
  WriteLn;
  
  // 检查目录是否存在
  WriteLn('2. 检查目录是否存在:');
  if DirectoryExists(GetCurrentDir()) then
    WriteLn('   当前目录存在')
  else
    WriteLn('   当前目录不存在');
    
  if DirectoryExists('non_existent_directory') then
    WriteLn('   non_existent_directory 存在')
  else
    WriteLn('   non_existent_directory 不存在');
  WriteLn;
  
  // 创建目录
  DirName := 'test_directory';
  WriteLn('3. 创建目录:');
  if CreateDir(DirName) then
    WriteLn('   成功创建目录: ', DirName)
  else
    WriteLn('   创建目录失败: ', DirName);
  WriteLn;
  
  // 遍历目录内容
  WriteLn('4. 当前目录内容:');
  FindResult := FindFirst('*', faAnyFile, SearchRec);
  try
    while FindResult = 0 do
    begin
      if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        if (SearchRec.Attr and faDirectory) <> 0 then
          WriteLn('   [目录] ', SearchRec.Name)
        else
          WriteLn('   [文件] ', SearchRec.Name);
      end;
      FindResult := FindNext(SearchRec);
    end;
  finally
    FindClose(SearchRec);
  end;
  WriteLn;
  
  // 删除目录
  WriteLn('5. 删除目录:');
  if RemoveDir(DirName) then
    WriteLn('   成功删除目录: ', DirName)
  else
    WriteLn('   删除目录失败: ', DirName);
  WriteLn;
  
  WriteLn('目录操作示例完成。');
  ReadLn;
end.